﻿// ***********************************************************************
// 项目名称         : 新疆嘉潤
// 程序名称         : UserController.cs
// 功能说明         ：用户管理控制器
// 编 写 人         : Xiaoping
// 创建日期         : 06-11-2014
// 软件版权         : 经纬科技
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using Jw.Core;
using Jw.Models;
using Jw.Web.App_Start;

namespace Jw.Web.Areas.Sys.Controllers
{
    public class UserController : BaseController
    {
        public ActionResult Index()
        {
            var sexItems = new List<dynamic>();
            sexItems.Add(new { value = "男", text = "男" });
            sexItems.Add(new { value = "女", text = "女" });
            var model = new
            {
                dataSource = new
                {
                    //性别
                    sexItems = sexItems,
                },
                form = new
                {
                    UserCode = "",
                    UserName = "",
                    RoleName = "",
                    OrganizeName = "",
                    OrganizeCode = "",
                    UserName1 = ""
                },
                defaultRow = new
                {
                    Password = "1234",
                    IsEnable = true,
                    Sex = "男"
                },
            };
            return View(model);
        }
    }

    public class UserApiController : ApiController
    {
        public dynamic Get(RequestWrapper request)
        {
            //<field name='b.OrganizeCode' cp='mapchild' extend='sys_userOrganizeMap,OrganizeCode,sys_organize' variable='OrganizeCode' ignoreEmpty='true'></field>
            request.LoadSettingXmlString(@"
<settings defaultOrderBy='CreateDate desc'>
    <select> UserCode,UserName,Description,Sex,MobileNumber,IsEnable,LoginCount,LastLoginDate,CreateDate,UserName as UserName1</select>
    <from> (select  a.*,isnull(b.OrganizeCode,'') as OrganizeCode from sys_user a left outer join  sys_userOrganizeMap b 
on a.usercode = b.usercode) c </from>
    <where>
        <field name='c.OrganizeCode'  cp='like'></field>    
        <field name='c.UserName' cp='StartWithPY'  ignoreEmpty='true'></field>
        <field name='c.UserName1'  cp='like'></field>    
        <field name='c.UserCode' cp='like'></field>  
    </where>
</settings>");
            var service = new sys_userService();

            var result = service.GetModelListWithPaging(request.ToParamQuery());

            //if (FormsAuth.GetUserData().UserName.ToLower() != "super")

            return result;
        }

        public dynamic GetSettingList(string id)
        {
            var pQuery = ParamQuery.Instance().AndWhere("UserCode", id);
            var service = new sys_userSettingService();
            return service.GetModelList(pQuery);
        }

        public dynamic GetOrganizeWithUserCheck(string id)
        {
            var service = new sys_userService();
            return service.GetUserOrganize(id);
        }

        public dynamic GetRoleWithUserCheck(string id)
        {
            var service = new sys_userService();
            return service.GetUserRole(id);
        }

        [System.Web.Http.HttpPost]
        public int PostResetPassword(string id)
        {
            var service = new sys_userService();
            return service.ResetUserPassword(id);
        }

        [System.Web.Http.HttpPost]
        public void Edit(dynamic data)
        {
            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
<settings>
    <table>sys_user</table>
    <where>
        <field name='UserCode' cp='equal'></field>
    </where>
</settings>");
            var service = new sys_userService();
            var result = service.Edit(null, listWrapper, data);
        }

        [System.Web.Http.HttpPost]
        public void EditUserOrganizes(string id, dynamic data)
        {
            var service = new sys_userService();
            service.SaveUserOrganizes(id, data as JToken);
        }

        [System.Web.Http.HttpPost]
        public void EditUserRoles(string id, dynamic data)
        {
            var service = new sys_userService();
            service.SaveUserRoles(id, data as JToken);
        }

        [System.Web.Http.HttpPost]
        public void EditUserSetting(dynamic data)
        {
            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
<settings>
    <table>sys_userSetting</table>
    <where>
        <field name='ID' cp='equal'></field>
    </where>
</settings>");
            var service = new sys_userSettingService();
            var result = service.Edit(null, listWrapper, data);
        }
    }
}
